package cn.idietrade.idietradegoodsmodule8002.service.goodsservice.impl;

import cn.idietrade.common.pojo.SecondGoods;
import cn.idietrade.idietradegoodsmodule8002.mapper.SecondGoodsMapper;
import cn.idietrade.idietradegoodsmodule8002.service.goodsservice.SecondGoodsService;
import cn.idietrade.idietradegoodsmodule8002.service.imgservice.SecondImgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-11-30 17:40
 * @description
 */
@Service
public class SecondGoodsServiceImpl implements SecondGoodsService {
    @Resource
    private SecondGoodsMapper secondGoodsMapper;

    @Resource
    private SecondImgService secondImgService;
    @Transactional
    @Override
    public int goodsRelease(String imgids,SecondGoods secondGoods) {
        int flag=0;
        int i = secondGoodsMapper.goodsRelease(secondGoods);
        int maxId = secondGoodsMapper.queryMaxId();
        int updataImg = secondImgService.updataImg(maxId, imgids);
        if(i>0&&maxId>0&&updataImg>0){
            flag=1;
        }
        return flag;
    }


    @Override
    public PageInfo<SecondGoods> queryConcerUserIdGoods(Integer pageNum, Integer pageSize,Integer userId) {
        if(pageNum==null||pageNum<=1){
            pageNum=1;
        }
        if(pageSize==null){
            pageSize=4;
        }
        PageHelper.startPage(pageNum,pageSize);

        //查询出当前用户关注的用户id
        List<Integer> integers = secondGoodsMapper.queryConcernUserId(userId);
        List<List<SecondGoods>> lists = new ArrayList<>();
        for (Integer integer : integers) {
            lists.add(secondGoodsMapper.queryConcerUserIdGoods(integer));
        }

        //转换为List<SecondGoods>类型进行分页返回
        List<SecondGoods> secondGoodsList = new ArrayList<>();
        for (List<SecondGoods> list : lists) {
            for (SecondGoods secondGoods : list) {
                secondGoodsList.add(secondGoods);
            }
        }
        PageInfo<SecondGoods> pageInfo=new PageInfo<>();
        pageInfo.setList(secondGoodsList);
        return pageInfo;
    }

    @Override
    public List<SecondGoods> queryGoodsAll(String keyword,Integer cityId) {
        return secondGoodsMapper.queryGoodsAll(keyword,cityId);
    }

    @Override
    public int queryMaxId() {
        return secondGoodsMapper.queryMaxId();
    }

    @Override
    public SecondGoods queryOneGoods(Integer goodsId) {
        return secondGoodsMapper.queryOneGoods(goodsId);
    }
}
